
class Query:
    def __init__(self, str):
        if not ":" in str:
            self.feature = str.strip()
            self.mn = None
            self.mx = None
        else:
            (self.feature, rng) = str.strip().split(":")
            if "-" in rng:
                (mn, mx) = rng.split("-")
                if mn=="START":
                    self.mn = None
                else:
                    self.mn = long(mn)
                if mx=="END":
                    self.mx = None
                else:
                    self.mx = long(mx)
            else:
                self.mn = self.mx = long(rng)
    def __str__(self):
        if self.mn == None:
            mn = "START"
        else:
            mn = str(self.mn)
        if self.mx == None:
            mx = "END"
        else:
            mx = str(self.mx)
        return "%s:%s-%s" % (self.feature, mn, mx)

def toTuple(query):
    if isinstance(query, Query):
        return (query.feature, query.mn, query.mx)
    return tuple(query)

if __name__=="__main__":
    s = []
    s.append("chr1:1-1000")
    s.append("chr1:1000")
    s.append("chr1")
    s.append("chr1:START-1000")
    s.append("chr1:45-END")
    for qstr in s:
        print qstr, "-->", Query(qstr)

